package com.yeyuanqi.hottop.scheduletask;

import com.yeyuanqi.hottop.service.*;
import com.yeyuanqi.hottop.untils.BasicLineChartDataUtil;
import com.yeyuanqi.hottop.untils.PieChartDataUtil;
import com.yeyuanqi.hottop.vo.BasicLineAndBarVO;
import com.yeyuanqi.hottop.vo.HotListVO;
import com.yeyuanqi.hottop.vo.NameAndValueVO;
import com.yeyuanqi.hottop.vo.PieChartVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;


/**
 * 将最新数据进行redis缓存同步
 * @author: yeyuanqi
 * @date: 2022/01/19
 */
@Component
@EnableAsync
public class DataRedisScheduleTask {

    private static final Logger logger = LoggerFactory.getLogger(DailyTopScheduleTask.class);

    @Autowired
    private TBaiduhotService tBaiduhotService;

    @Autowired
    private TJinrihotService tJinrihotService;

    @Autowired
    private TTxhotService tTxhotService;

    @Autowired
    private TWeibohotService tWeibohotService;

    @Autowired
    private PieChartDataUtil pieChartDataUtil;

    @Autowired
    private BasicLineChartDataUtil basicLineChartDataUtil;


    @Autowired
    RedisService redisService;

    @Scheduled(cron = "10 0,15,30,45 * * * ?")
    public void bdLineAndBarChartDataSync(){

        //获取数据
        List<HotListVO> bdhotListVOS = tBaiduhotService.selectNewHot();
        //封装格式
        BasicLineAndBarVO bdLineAndBarChartData = basicLineChartDataUtil.getBasicLineAndBarChartHotData(bdhotListVOS);
        redisService.lRightPop("bdLineAndBarData");
        redisService.lPushAll("bdLineAndBarData",bdLineAndBarChartData);
        PieChartVO pieHotData = pieChartDataUtil.getPieHotData(bdhotListVOS);
        redisService.lRightPop("bdPieData");
        redisService.lPushAll("bdPieData",pieHotData);
        redisService.del("baiduNewsHot");
        redisService.set("baiduNewsHot", bdhotListVOS);

        logger.info("百度缓存数据更新");
    }


    @Scheduled(cron = "10 0,15,30,45 * * * ?")
    public void jrLineAndBarChartDataSync(){

        List<HotListVO> jrhotListVOS = tJinrihotService.selectNewHot();
        BasicLineAndBarVO jrLineAndBarChartData = basicLineChartDataUtil.getBasicLineAndBarChartHotData(jrhotListVOS);
        redisService.lRightPop("jrLineAndBarData");
        redisService.lPushAll("jrLineAndBarData",jrLineAndBarChartData);
        PieChartVO pieHotData = pieChartDataUtil.getPieHotData(jrhotListVOS);
        redisService.lRightPop("jrPieData");
        redisService.lPushAll("jrPieData",pieHotData);

        redisService.del("jinriNewsHot");
        redisService.set("jinriNewsHot", jrhotListVOS);
        logger.info("今日头条缓存数据更新");
    }

    @Scheduled(cron = "13 0,15,30,45 * * * ?")
    public void txLineAndBarChartDataSync(){
        List<HotListVO> txhotListVOS = tTxhotService.selectNewHot();
        BasicLineAndBarVO txLineAndBarChartData = basicLineChartDataUtil.getBasicLineAndBarChartHotData(txhotListVOS);
        redisService.lRightPop("txLineAndBarData");
        redisService.lPushAll("txLineAndBarData",txLineAndBarChartData);
        PieChartVO pieHotData = pieChartDataUtil.getPieHotData(txhotListVOS);
        redisService.lRightPop("txPieData");
        redisService.lPushAll("txPieData",pieHotData);

        redisService.del("tengxunNewsHot");
        redisService.set("tengxunNewsHot", txhotListVOS);
        logger.info("腾讯新闻缓存数据更新");
    }


    @Scheduled(cron = "13 0,15,30,45 * * * ?")
    public void wbLineAndBarChartDataSync(){
        List<HotListVO> wbhotListVOS = tWeibohotService.selectNewHot();
        BasicLineAndBarVO wbLineAndBarChartData = basicLineChartDataUtil.getBasicLineAndBarChartHotData(wbhotListVOS);
        redisService.lRightPop("wbLineAndBarData");
        redisService.lPushAll("wbLineAndBarData",wbLineAndBarChartData);
        PieChartVO pieHotData = pieChartDataUtil.getPieHotData(wbhotListVOS);
        redisService.lRightPop("wbPieData");
        redisService.lPushAll("wbPieData",pieHotData);

        redisService.del("weiboNewsHot");
        redisService.set("weiboNewsHot", wbhotListVOS);
        logger.info("微博图表数据更新");
    }
}
